package com.zr.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.zr.model.Cdisstation;
import com.zr.model.Cgoodstype;
import com.zr.model.Clogstate;
import com.zr.model.Corder;
import com.zr.model.Costate;
import com.zr.model.Cotype;
import com.zr.model.Cuser;
import com.zr.service.OrderService;

import net.sf.json.JSONObject;

@Controller
@Scope(value="prototype")
public class OrderAction extends ActionSupport implements ServletResponseAware,SessionAware,ServletRequestAware{
    private HttpServletResponse resp;
	private Map<String ,Object> session;
    private HttpServletRequest req;
    private int page;
    private int rows;  
    private Corder corder;
    private Cgoodstype goodstype;
    private Clogstate logstate;
    private Costate ostate;
    private Cotype otype;
    private Cdisstation disstation;
    
	@Override
	public void setSession(Map<String, Object> arg0) {
		this.session=arg0;
	}
	@Override
	public void setServletResponse(HttpServletResponse arg0) {
		this.resp=arg0;
	}
	public int getPage() {
		return page;
	}
	public void setPage(int page) {
		this.page = page;
	}
	public int getRows() {
		return rows;
	}
	public void setRows(int rows) {
		this.rows = rows;
	}
	
	public Corder getCorder() {
		return corder;
	}
	public void setCorder(Corder corder) {
		this.corder = corder;
	}
	
	public Cgoodstype getGoodstype() {
		return goodstype;
	}
	public void setGoodstype(Cgoodstype goodstype) {
		this.goodstype = goodstype;
	}
	public Clogstate getLogstate() {
		return logstate;
	}
	public void setLogstate(Clogstate logstate) {
		this.logstate = logstate;
	}
	public Costate getOstate() {
		return ostate;
	}
	public void setOstate(Costate ostate) {
		this.ostate = ostate;
	}
	public Cotype getOtype() {
		return otype;
	}
	public void setOtype(Cotype otype) {
		this.otype = otype;
	}
	
	public Cdisstation getDisstation() {
		return disstation;
	}
	public void setDisstation(Cdisstation disstation) {
		this.disstation = disstation;
	}
	
	public Map<String, Object> getSession() {
		return session;
	}
	@Override
	public void setServletRequest(HttpServletRequest arg0) {
		this.req=arg0;
	}
	
	@Resource
	private OrderService orderService;
	//展示订单信息
	public void showOrder(){
		try {
			
			req.setCharacterEncoding("utf8");
			resp.setCharacterEncoding("utf8");
			Cuser user=(Cuser)session.get("cuser");
			JSONObject js=orderService.getOrderInfo(page, rows, user.getUid());
			PrintWriter pw;
			try {
				pw = resp.getWriter();
				pw.write(js.toString());
				pw.flush();
				pw.close();
			} catch (IOException e) {
				e.printStackTrace(); 
			}
		} catch (UnsupportedEncodingException e1) {
			e1.printStackTrace();
		}
	}
	//客户下定单
   public void insertOrder(){
	   resp.setCharacterEncoding("utf8");
	   System.out.println(ostate.getOstate());
	   System.out.println(goodstype.getGtname());
	   System.out.println(otype.getOtype());
	   System.out.println(logstate.getLogstate());
	   Cuser user=(Cuser)session.get("cuser");
	   int i=orderService.insertOrder(corder, goodstype, logstate, ostate, otype, user.getUid());
     System.out.println(i);
	   try {
		PrintWriter pw= resp.getWriter();
		if(i>0){
			pw.write("1");
		}else{
			pw.write("0");
		}
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	   
   }
   //配送点下定单
   public void psinsertOrder(){
	   resp.setCharacterEncoding("utf8");
	   
	   
   }
   //确认订单
   public void orderSure(){
	   resp.setCharacterEncoding("utf8");
	   JSONObject js=orderService.getCurrentOrderInfo(page, rows, disstation.getDisid());
	   try {
		PrintWriter pw=resp.getWriter();
		pw.write(js.toString());
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
   }


}
